package com.hx.er.action.admin.food;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.springframework.beans.factory.annotation.Autowired;

import com.hx.er.action.base.ErAdminAction;
import com.hx.er.bean.base.RestraurantBean;
import com.hx.er.bean.detail.FoodDetailBean;
import com.hx.er.common.exception.ErException;
import com.hx.er.common.system.ErLanguages;
import com.hx.er.common.util.FileUtil;
import com.hx.er.service.admin.IAdminFoodService;

public class AdminFoodDeleteAction extends ErAdminAction {

	// 序列化ID
	private static final long serialVersionUID = -5885896269081800347L;

	private long foodId;
	private FoodDetailBean foodDetailBean;

	// 餐厅对象
	private RestraurantBean restBean;
	// 餐厅编号
	private long restId;

	// 多语言标签名
	private String labelLang1;
	private String labelLang2;
	private String labelLang3;
	private String labelLang4;
	private String labelLang5;

	@Autowired
	private IAdminFoodService adminFoodService;

	public String init() throws IOException {
		// 初始化页面
		this.initPage();

		return SUCCESS;
	}

	public String delete() throws IOException {
		if (this.foodDetailBean == null) {
			this.foodDetailBean = new FoodDetailBean();
		}
		
		this.foodDetailBean.setFoodId(this.foodId);
		this.foodDetailBean.setUpdateUserId(super.getUserInfoLogin().getUserId());

		int count = this.adminFoodService.deleteFood(this.foodDetailBean);

		if (count == 0) {
			// 初始化页面
			this.initPage();
			// 添加错误信息
			this.addActionError(super.getText(DELETE_FAILURE_MSG));
			return INPUT;
		} else {
			// 添加成功信息
			this.addActionMessage(super.getText(DELETE_SUCCESS_MSG));
			return SUCCESS;
		}
	}

	/**
	 * 检查文件，如果不存在则重新生成
	 * 
	 * @throws ErException
	 * @throws IOException
	 */
	private void checkPictureFile() throws IOException {
		// 如果食品对象不存在，则掠过该方法
		if (this.foodDetailBean == null) {
			return;
		}

		// 声明输出流
		OutputStream fos = null;

		// 循环处理列表中所有对象
		String root = request.getServletContext().getRealPath("/");
		File pictureFile = new File(root, this.foodDetailBean.getPicturePath());

		// 如果所在目录不存在，则先创建
		FileUtil.createDir(pictureFile);

		// 如果文件不存在，则写文件
		if (!pictureFile.exists()) {
			fos = new FileOutputStream(pictureFile);
			fos.write(this.foodDetailBean.getPictureFile());
			if (fos != null) {
				fos.close();
			}
		}
	}

	private void initPage() throws IOException {
		// 查询食品详细信息
		this.foodDetailBean = this.adminFoodService.getFoodAliveById(this.foodId);
		this.restId = this.foodDetailBean.getRestId();
		
		// 检索餐厅
		this.restBean = this.adminFoodService.getRestById(this.restId);

		// 语言
		if (this.restBean.getLangId1() != 0) {
			this.labelLang1 = ErLanguages.getLocale(this.restBean.getLangId1()).getLanguage().toUpperCase();
		}
		if (this.restBean.getLangId2() != 0) {
			this.labelLang2 = ErLanguages.getLocale(this.restBean.getLangId2()).getLanguage().toUpperCase();
		}
		if (this.restBean.getLangId3() != 0) {
			this.labelLang3 = ErLanguages.getLocale(this.restBean.getLangId3()).getLanguage().toUpperCase();
		}
		if (this.restBean.getLangId4() != 0) {
			this.labelLang4 = ErLanguages.getLocale(this.restBean.getLangId4()).getLanguage().toUpperCase();
		}
		if (this.restBean.getLangId5() != 0) {
			this.labelLang5 = ErLanguages.getLocale(this.restBean.getLangId5()).getLanguage().toUpperCase();
		}

		// 检查图片
		this.checkPictureFile();
	}

	public long getFoodId() {
		return foodId;
	}

	public void setFoodId(long foodId) {
		this.foodId = foodId;
	}

	public FoodDetailBean getFoodDetailBean() {
		return foodDetailBean;
	}

	public void setFoodDetailBean(FoodDetailBean foodDetailBean) {
		this.foodDetailBean = foodDetailBean;
	}

	public RestraurantBean getRestBean() {
		return restBean;
	}

	public void setRestBean(RestraurantBean restBean) {
		this.restBean = restBean;
	}

	public String getLabelLang1() {
		return labelLang1;
	}

	public void setLabelLang1(String labelLang1) {
		this.labelLang1 = labelLang1;
	}

	public String getLabelLang2() {
		return labelLang2;
	}

	public void setLabelLang2(String labelLang2) {
		this.labelLang2 = labelLang2;
	}

	public String getLabelLang3() {
		return labelLang3;
	}

	public void setLabelLang3(String labelLang3) {
		this.labelLang3 = labelLang3;
	}

	public String getLabelLang4() {
		return labelLang4;
	}

	public void setLabelLang4(String labelLang4) {
		this.labelLang4 = labelLang4;
	}

	public String getLabelLang5() {
		return labelLang5;
	}

	public void setLabelLang5(String labelLang5) {
		this.labelLang5 = labelLang5;
	}

	public long getRestId() {
		return restId;
	}

	public void setRestId(long restId) {
		this.restId = restId;
	}
}
